import type { UploadImage, UserData } from "@/types/user";
import { request } from "@/utils/http";

export type CodeType =
  | "login"
  | "register"
  | "changeMobile"
  | "forgetPassword"
  | "bindMobile";

export interface GetCodeParams {
  mobile: string;
  type: CodeType;
}

export interface getCodeData {
  code: string;
}
export const getCodeApi = (params: GetCodeParams) => {
  return request.get<null, getCodeData>("/code", { params });
};
/** 参数类型定义 */
interface LoginCodeParams {
  /** 手机号 */
  mobile: string;
  /** 验证码 */
  code: string;
}

export interface LoginCodeData {
  /** 用户名 */
  account?: string;
  /**
   * 头像
   */
  avatar?: string;
  /**
   * 用户id
   */
  id: string;
  /**
   * 脱敏手机号，带星号的手机号
   */
  mobile?: string;
  /**
   * refreshToken
   */
  refreshToken: string;
  /**
   * token
   */
  token: string;
}

export const loginByMobileApi = (params: LoginCodeParams) => {
  return request.post<null, LoginCodeData>("/login", params);
};
interface LoginPasswordParams {
  mobile: string;
  password: string;
}
export const loginByPasswordApi = (params: LoginPasswordParams) => {
  return request.post<null, LoginCodeData>("/login", params);
};

export const userInfoApi = () => {
  return request.get<null, UserData>("/patient/myUser");
};


export const uploadImageApi = (filePath: string) => {
  // 小程序需调用通过 uni.uploadFile 上传资源，客户端发起一个 POST 请求
  return request.upload<null, UploadImage>('/upload', {
    name: 'file', // 服务器的 key
    filePath: filePath, // 上传文件资源的路径(沙箱路径)，上传时会自动转换成二进制
  })
}


/**
 * 三方支付（暂时只支持支付宝支付）
 */
export const orderPayApi = (data: PayParams) => {
  return request.post<null, { payUrl: string }>('/patient/consult/pay', data)
}